//城市数据处理
var cityArr = [];
for(var i=0,len=cityData.length;i<len;i++){
    for(var j=0,lens=cityData[i].list.length;j<lens;j++){
        var listArr = cityData[i].list[j];
        cityArr.push(listArr.name +"|"+ listArr.cityCode +"|"+ listArr.enName)
    }
}
//console.log(cityArr);

// 城市选择框显示
var inCity = new Object();
inCity.cssParents = function(){ //城市显示
    inCity.top = placeThis.offset().top+27;  //城市选择框  top位置
    inCity.left = placeThis.offset().left;   //城市选择框  left位置
    $(inCity.id).css({"top":inCity.top+"px","left":inCity.left+"px","width":inCity.width+"px","height":inCity.height+"px"});
}
inCity.place = function (e) {
    e.click(function(){
        placeThis = $(this);
        //城市显示
        inCity.cssParents();
        $(inCity.id).show();
        return false;
    })
}
inCity.destination = function (e){
    e.click(function(){
        placeThis = $(this);
        //城市显示
        inCity.cssParents();
        $(inCity.id).show();
        return false;
    })
}

/* 城市HTML模板 */
inCity._template = [
    '<h1>请选择城市</h1>',
    '<div class="screen">',
    '<a href="javascript:void(0)" class="shar">热门</a>',
    '<a href="javascript:void(0)">ABCDEFG</a>',
    '<a href="javascript:void(0)">HIJKL</a>',
    '<a href="javascript:void(0)">MNOPQRST</a>',
    '<a href="javascript:void(0)">WXYZ</a>',
    '</div>',
    '<div class="city_pos">',
    '<div class="city_a_le1">',
    '</div>',
    '<div class="city_a_le1" style="display: none">',
    '</div>',
    '<div class="city_a_le1" style="display: none">',
    '</div>',
    '<div class="city_a_le1" style="display: none">',
    '</div>',
    '<div class="city_a_le1" style="display: none">',
    '</div>',
    '</div>'
];
/* 所有城市数据,可以按照格式自行添加（北京|beijing|bj），前16条为热门城市 */

inCity.allCity = cityArr;

/* 正则表达式 筛选中文城市名、拼音、首字母 */

inCity.regEx = /^([\u4E00-\u9FA5\uf900-\ufa2d]+)\|(\w+)\|(\w)\w*$/i;
inCity.regExChiese = /([\u4E00-\u9FA5\uf900-\ufa2d]+)/;

(function () {
    var citys = inCity.allCity, match, letter,newArr,
        regEx = inCity.regEx,
        reg2 = /^[a-g]$/i, reg3 = /^[h-l]$/i, reg4 = /^[m-t]$/i, reg5 = /^[w-z]$/i;
    if (!inCity.oCity) {
        inCity.oCity = {hot:{},ABCDEFG:{}, HIJKL:{}, MNOPQRST:{}, WXYZ:{}};
        //console.log(citys.length);
        for (var i = 0, n = citys.length; i < n; i++) {
            match = regEx.exec(citys[i]); //exec
            letter = match[3].toUpperCase(); //转换字母为大写
            newArr = match[1] +","+ match[2];//拼接name/code
            if (reg2.test(letter)) { //test检测一个字符串是否匹配某个模式
                if (!inCity.oCity.ABCDEFG[letter]) inCity.oCity.ABCDEFG[letter] = [];
                inCity.oCity.ABCDEFG[letter].push(newArr);
            } else if (reg3.test(letter)) {
                if (!inCity.oCity.HIJKL[letter]) inCity.oCity.HIJKL[letter] = [];
                inCity.oCity.HIJKL[letter].push(newArr);
            } else if (reg4.test(letter)) {
                if (!inCity.oCity.MNOPQRST[letter]) inCity.oCity.MNOPQRST[letter] = [];
                inCity.oCity.MNOPQRST[letter].push(newArr);
            } else if (reg5.test(letter)) {
                if (!inCity.oCity.WXYZ[letter]) inCity.oCity.WXYZ[letter] = [];
                inCity.oCity.WXYZ[letter].push(newArr);
            }
            /* 热门城市 前16条 */
            if(i<16){
                if(!inCity.oCity.hot['hot']) inCity.oCity.hot['hot'] = [];
                inCity.oCity.hot['hot'].push(newArr);
            }
        }
    }
})();
// 热门城市
inCity.Hot = function(cityA){
    var ckey,odda,sortKey,str,odda=[],abc=[],key,regEx = inCity.regEx,oCity = inCity.oCity,len,leni;
    for(key in oCity){
        sortKey=[];
        for(ckey in oCity[key]){
            sortKey.push(ckey);
            // ckey按照ABCDEDG顺序排序
            sortKey.sort();
        }
        for(var j=0,k = sortKey.length;j<k;j++){
            odda = [];
            abc=[];
            for(var i=0,n=oCity[key][sortKey[j]].length;i<n;i++){
                var _this = oCity[key][sortKey[j]][i].split(",");
                if(key == 'hot'){
                    $(inCity.id).find(inCity.Children).eq(0).append('<a href="javascript:void(0)" data-code="'+_this[1]+'">' +_this[0]+ '</a>');
                    odda.push(str);
                }else{
                    str = '<a href="javascript:void(0)" data-code="'+_this[1]+'">' +_this[0]+ '</a>';
                    inCity.arrRepeat(abc,sortKey,j); //获取字母
                    odda.push(str);
                    len = n;
                    leni = i;
                }

            }
            inCity.cityPinyin(leni,len,key,abc,odda);
        }
    }
}

// 按拼音排序
inCity.cityPinyin = function(leni,len,key,abc,odda){
    if(leni != undefined && key != 'hot'){
        if(len-1 == leni){
            var one;
            switch (key)
            {
                case 'ABCDEFG':
                    one = 1;
                    break;
                case 'HIJKL':
                    one = 2;
                    break;
                case 'MNOPQRST':
                    one = 3;
                    break;
                case 'WXYZ':
                    one = 4;
                    break;
            }
            $(inCity.id).find(inCity.Children).eq(one).append('<div class="Letter">'
                +'<span>'+abc[0]+'</span>'
                +'<div>'
                +odda.join('')
                +'</div>'
                +'</div>');
        }
    }
}

// 数组去重
inCity.arrRepeat = function(abc,sortKey,j){
    var nab = sortKey[j];
    for(var i in abc){
        if(abc[i] == nab){
            return nab = 1;
        }
    }
    if(nab != 1){
        abc.push(sortKey[j])
    }
}
// 城市切换
inCity.payment = function($this){
    var ind = $this.index();
    $this.siblings().removeClass("shar");
    $this.addClass("shar");
    $this.parent().next().children().hide();
    $this.parent().next().children().eq(ind).show();
}
// 给input赋值
inCity.cityClick = function ($this) {
    $(".city_a_le1 a").click(function () {
        var a_city = $(this).text();  //当前选择的城市
        $(inCity.id).hide();  //隐藏城市选择框 
        placeThis.val($(this).attr("data-code")).siblings("em").text(a_city);  //赋值
        return false;
    })
}